import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/app/yhpc/YhpcViewModel.dart';
import 'package:flutter_ahj_app/base/basewidget/view/BaseWidget.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JText.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
import 'package:flutter_ahj_app/base/utils/mTopBar.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';
import 'package:flutter_ahj_app/base/widget/LoadingWidget.dart';
import 'package:flutter_ahj_app/base/widget/NoneData.dart';

class ChoosePcdxView extends BaseStatefulWidget {
  @override
  State<StatefulWidget> initState() {
    // TODO: implement initState
    // throw UnimplementedError();
    return ChoosePcdxViewState();
  }
}

class ChoosePcdxViewState extends BaseState<YhpcViewModel, ChoosePcdxView> {
  TextEditingController _controller = TextEditingController(text: '');
  List _pcdxList;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    Future.delayed(Duration.zero, () {
      _getData();
    });
  }

  _getData() {
    var map = {'riskPointName': _controller.text};
    viewModel.getPcnr(map);
    viewModel.getPcnrStream().listen((event) {
      if (event['code'] == '0') {
        setState(() {
          _pcdxList = [];
          _pcdxList = event['data']['records'];
        });
      } else {
        ToastUtils.showToast(event['msg']);
      }
    }, onError: (e) {
      print(e);
    });
  }

  @override
  Widget initWidget() {
    return Scaffold(
      backgroundColor: Color(0xfff4f5f6),
      appBar: MTopBar(
        elevation: 0,
        title: '选择排查对象',
      ).create(),
      body: _pcdxList == null
          ? LoadingWidget()
          : _bodyView(),
    );
  }

  _bodyView() {
    return Column(
      children: [
        _searchLayout(),
        Expanded(
          child: _pcdxList.length == 0
              ? NoneData()
              : Container(
            margin: EdgeInsets.fromLTRB(
                Dimens.dimens16, 0, Dimens.dimens16, Dimens.dimens16),
            child: SingleChildScrollView(
              child: ListView.builder(
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  itemCount: _pcdxList.length,
                  itemBuilder: (context, index) {
                    return InkWell(
                      onTap: () {
                        Navigator.pop(context, _pcdxList[index]);
                      },
                      child: Container(
                        padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
                        decoration: BoxDecoration(
                          color: SQColor.white,
                          borderRadius: BorderRadius.circular(6),
                        ),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Container(
                              margin: EdgeInsets.only(top: 10),
                              child: JText(
                                _pcdxList[index]['riskPointName'] ?? '',
                                fontWeight: FontWeight.bold,
                                fontSize: 16,
                              ),
                            ),
                            Container(
                              margin: EdgeInsets.only(top: 10),
                              child: JText(
                                  '排查对象类型：${_pcdxList[index]['riskPointTypeName'] ?? ''}'),
                            ),
                            Container(
                              margin: EdgeInsets.only(top: 10),
                              child: JText(
                                  '场所/位置：${_pcdxList[index]['districtName'] ?? ''}'),
                            ),
                            Container(
                              margin: EdgeInsets.only(top: 10, bottom: 10),
                              child: JText(
                                  '管控责任部门：${_pcdxList[index]['orgName'] ?? ''}'),
                            ),
                          ],
                        ),
                      ),
                    );
                  }),
            ),
          ),
        ),
      ],
    );
  }

  _searchLayout() {
    return Container(
      margin: EdgeInsets.all(Dimens.dimens16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(6.0),
      ),
      child: Row(
        children: [
          Container(
            width: 20,
            height: 20,
            margin: EdgeInsets.only(left: 10),
            child: Icon(
              Icons.search,
              color: Color(0xffaaaaaa),
            ),
          ),
          Expanded(
            child: Container(
              height: Dimens.dimens36,
              child: TextField(
                controller: _controller,
                maxLines: 1,
                onSubmitted: (value) {
                  _getData();
                },
                decoration: InputDecoration(
                  hintText: "输入排查对象名称、场所/位置搜索",
                  hintStyle: TextStyle(fontSize: Dimens.textSize28),
                  fillColor: Colors.white,
                  //filled = true 时为背景颜色
                  filled: true,
                  contentPadding: EdgeInsets.fromLTRB(
                      Dimens.dimens16, 0, Dimens.dimens16, 0),
                  enabledBorder: OutlineInputBorder(
                    borderRadius:
                        BorderRadius.all(Radius.circular(Dimens.dimens4)), //圆角
                    borderSide: BorderSide(color: Colors.white, width: 1), //边框
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderRadius:
                        BorderRadius.all(Radius.circular(Dimens.dimens4)), //圆角
                    borderSide: BorderSide(color: Colors.white, width: 1), //边框
                  ),
                  suffixIcon: _controller.text.length > 0
                      ? new IconButton(
                          icon: Icon(
                            Icons.close,
                            color: SQColor.per38Black,
                            size: Dimens.dimens20,
                          ),
                          onPressed: () {
                            setState(() {
                              _controller.clear();
                              _getData();
                            });
                          })
                      : null,
                ),
              ),
            ),
          ),
          Container(
            width: 1,
            color: SQColor.black_BDBDBD,
            height: Dimens.dimens16,
            margin: EdgeInsets.only(right: Dimens.dimens16),
          ),
          GestureDetector(
            onTap: () {
              FocusScope.of(context).requestFocus(FocusNode());
              _getData();
            },
            child: Container(
              padding: EdgeInsets.only(right: Dimens.dimens16),
              height: Dimens.dimens48,
              alignment: Alignment.centerRight,
              child: Text("搜索",
                  style: TextStyle(
                      color: Color(0xff165DFF), fontSize: Dimens.textSize28)),
            ),
          ),
        ],
      ),
    );
  }
}
